package com.achievement.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.achievement.domain.dto.honor.HonorAuthorDTO;
import com.achievement.domain.entity.HonorAuthorEntity;
import com.achievement.domain.vo.honor.HonorAuthorVO;
import com.achievement.mapper.HonorAuthorMapper;
import com.achievement.mappings.HonorAuthorMapping;
import com.achievement.service.IHonorAuthorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class HonorAuthorServiceImpl extends ServiceImpl<HonorAuthorMapper, HonorAuthorEntity> implements IHonorAuthorService {


    @Autowired
    private HonorAuthorMapper honorAuthorMapper;
    @Autowired
    private HonorAuthorMapping mapping;

    @Override
    public List<HonorAuthorVO> honorAuthorListByHonorId(String honorId) {
        List<HonorAuthorVO> voList = honorAuthorMapper.honorAuthorListByHonorId(honorId).stream().map(mapping::entity2Vo).collect(Collectors.toList());
        return voList;
    }
    @Override
    public List<HonorAuthorEntity> getListByConditions(HonorAuthorDTO honorAuthorDTO) {
        QueryWrapper<HonorAuthorEntity> queryWrapper = new QueryWrapper();
        List<HonorAuthorEntity> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public boolean bitchAdd(List<HonorAuthorDTO> honorAuthorDTOs,String honorId){
        List<HonorAuthorEntity> entityList = honorAuthorDTOs.stream().map(mapping::dto2Entity).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(entityList)){
            entityList.forEach(a->{
                a.setId(UUID.randomUUID().toString().replace("-",""));
                a.setHonorId(honorId);
            });
            return this.saveBatch(entityList);
        }
        return  true;
    }

    @Override
    public HonorAuthorVO getInfoById(String id) {
        HonorAuthorEntity entity = this.getById(id);
        return mapping.entity2Vo(entity);
    }

    @Override
    public boolean add(HonorAuthorDTO dto) {
        HonorAuthorEntity entity = mapping.dto2Entity(dto);
        return this.save(entity);
    }


    @Override
    public boolean update(HonorAuthorDTO dto) {
        HonorAuthorEntity entity = mapping.dto2Entity(dto);
        return this.update(entity, null);
    }

    @Override
    public boolean batchRemove(List<String> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public int deleteAuthorListByHonorId(String honorId){
        return honorAuthorMapper.deleteAuthorListByHonorId(honorId);
    }
}
